import java.io.ObjectOutputStream;
import java.util.Scanner;

/**
 * @author 乐心湖
 * @version 1.0
 * @date 2021/9/8 13:18
 */
public class ClientSend implements Runnable {
    private ObjectOutputStream oos;
    private boolean first = true;

    @Override
    public void run() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您的昵称：");
        String name = scanner.nextLine();
        while (true) {
            try {
                //首次发送或重连服务端
                if (first || MyClientSocket.isOosUpdated()) {
                    //更新输出流
                    oos = new ObjectOutputStream(MyClientSocket.getSocket().getOutputStream());
                    oos.writeObject(name);
                    first = false;
                    MyClientSocket.setOosUpdated(false);
                }
                String msg = scanner.nextLine();
                //消息为空则无视，流被更新了也无视
                if (!MyClientSocket.isOosUpdated() && msg.trim().length() > 0) {
                    oos.writeObject(msg);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
